package com.ccit.utils;

import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SchedulerConfig {

    @Autowired
    private DataAddTask dataAddTask;

    @Bean
    public JobDetail dataAddJobDetail() {
        return JobBuilder.newJob(DataAddTask.class)
                .withIdentity("dataAddJob")
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger dataAddJobTrigger() {
        // 每天 3 点 45 分执行的 Cron 表达式为 "0 45 3 * * ?"
        String cronExpression = "0 00 23 * * ?";

        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);

        return TriggerBuilder.newTrigger()
                .forJob(dataAddJobDetail())
                .withIdentity("dataAddTrigger")
                .withSchedule(scheduleBuilder)
                .build();
    }

    @Bean
    public Scheduler scheduler(Trigger dataAddJobTrigger, JobDetail dataAddJobDetail) throws SchedulerException {
        SchedulerFactory schedulerFactory = new org.quartz.impl.StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.scheduleJob(dataAddJobDetail, dataAddJobTrigger);
        scheduler.start();
        return scheduler;
    }
}